//
//  LMWebTableViewCell.m
//  LMLabelSelect
//
//  Created by xielei on 2022/12/5.
//

#import "LMWebTableViewCell.h"
#import <Masonry/Masonry.h>
#define KSCREENW [UIScreen mainScreen].bounds.size.width
#define KSCREENH [UIScreen mainScreen].bounds.size.height
@interface LMWebTableViewCell()<WKNavigationDelegate>
@property (strong, nonatomic) LMWebTableViewCellModel *cellModel;
@property (strong, nonatomic) UIView *bgView;
@end

@implementation LMWebTableViewCell

+ (NSString*)reuseIdentifier{
    return NSStringFromClass([LMWebTableViewCell class]);
}


- (void)setModel:(id<LMViewModelProtocol>)viewModel Index:(NSInteger)index{
    
    self.cellModel = viewModel;
    
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.loadWebView];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(KSCREENH);
    }];
    
    [self.loadWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(self.bgView);
    }];
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.cellModel.model.url]];
    [self.loadWebView loadRequest:request];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (UIView*)bgView{
    if (!_bgView){
        _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    }
    return _bgView;
}


- (WKWebView*)loadWebView{
    if (!_loadWebView) {
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width','initial-scale=1.0', 'maximum-scale=1.0', 'minimum-scale=1.0', 'user-scalable=no'); document.getElementsByTagName('head')[0].appendChild(meta);$('meta[name=description]').remove(); $('head').append( '' );";

        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
           [wkUController addUserScript:wkUScript];
        WKWebViewConfiguration *config  = [[WKWebViewConfiguration alloc]init];
        config.userContentController = wkUController;
        _loadWebView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
        _loadWebView.navigationDelegate = self;
        [_loadWebView.scrollView setShowsVerticalScrollIndicator:NO];
        [_loadWebView.scrollView setShowsHorizontalScrollIndicator:NO];
        _loadWebView.scrollView.scrollEnabled = NO;
    }
    return _loadWebView;
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    // 禁止放大缩小
       NSString *injectionJSString = @"var script = document.createElement('meta');"
       "script.name = 'viewport';"
       "script.content=\"width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\";"
       "document.getElementsByTagName('head')[0].appendChild(script);";
       [webView evaluateJavaScript:injectionJSString completionHandler:nil];
    
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id result, NSError *_Nullable error) {
        float height = [result floatValue];
        self.cellModel.height = height;
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(height);
        }];
//        [self.delegate getWkWebViewHeight:height];
    }];
}

@end


@implementation LMWebTableViewCellModel

- (CGFloat)viewHeight{
    return self.height;
}

- (Class)viewClass{
    return [LMWebTableViewCell class];
}

- (void)setModel:(LMWebCellModel *)model{
    _model = model;
    
    self.height = KSCREENH;
}
@end
